import logging

from pyleo.lib.base import *
from pyleo.model.lfs_aggregate import *
import webhelpers as h
import webhelpers.feedgenerator
from pyleo.controllers.main import MainController
from pyleo.lib.formatTime import formatTime

log = logging.getLogger(__name__)

class BlogController(BaseController):

    def _getem(self):
        if c.path.strip('/')==g.blog_path: 
            c.aggregations,l=lfs_aggregate(g.lfs,c.path,limit=int(g.blog_recent_count))
        else: c.aggregations,l=lfs_aggregate(g.lfs,c.path)

    def index(self):
       
        self._getem()
        if len(c.aggregations)==0: 
            Main=MainController()
            return Main.index()
        else: 
            for a in c.aggregations:
                a['time']=formatTime(a['creation_time'])
            return render('blogEntries')
    
    def atom(self):
       c.path=g.blog_path
       self._getem()
       feed=h.feedgenerator.Atom1Feed(g.blog_title,h.url_for(),g.site_sub_title)
       for e in c.aggregations:
           feed.add_item(e['title'],e['url'],e['content'])
       return feed.writeString('utf-8')

    def atomCategories(self):
        pass